package com.kepe.dragon.service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

import org.coral.server.game.helper.ResourceType;
import org.coral.server.game.helper.log.NatureEnum;
import org.coral.server.game.module.item.domain.IItem;
import org.coral.server.game.module.item.domain.Item;
import org.coral.server.game.module.item.domain.ItemDomain;
import org.coral.server.game.module.item.manager.ItemManager;
import org.coral.server.game.module.item.proto.*;
import org.coral.server.game.module.player.service.IPlayerService;
import org.coral.server.game.module.resource.IResourceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * Item控制器
 */
@Service
public class ItemService implements IItemService , IResourceService{
	
	private static final Logger log = LoggerFactory.getLogger(ItemService.class);
	
	@Autowired private IPlayerService playerService;
	
	@Autowired private ItemManager itemManager;
	
	/**
	 * 登陆
	 */
	public void onLogin() {
		ItemDomain domain = itemManager.getDomain(playerId);
		Collection<item> beans = domain.getBeans();
		//FSC todo somthing...
		//Codes for proto
		//playerService.sendMessage(playerId, ack);
	}
	
	/**
	 * 当玩家离线,移除掉道具模块数据
	 * @param playerId
	 */
	public void onLogout(long playerId) {
		itemManager.remove(playerId);
	}
	
	/**
	 * 更新信息
	 */
	public void responseItemInfo(ItemDomain domain) {
		Collection<Item> beans = domain.getBeans();
		try {
			for (Item item : beans) {
				//resp.addArtifactlist(item.toProto());
			}
			//playerService.sendMessage(playerId, resp);
		} catch (Exception e) {
			e.printStackTrace();
			log.info("responseItemInfo error, playerId:{}", domain.getPlayerId());
			log.error("responseItemInfo error, e:", e);
		}
	}
	
	/////////////业务逻辑//////////////////
	
	
	/////////////接口方法////////////////////////
	
	@Override
	public int resType() {
		return ResourceType.Item.getType();
	}
	
	@Override
	public boolean checkAdd(long playerId, Integer configId, Integer value) {
		ItemDomain domain = itemManager.getDomain(playerId);
		if (domain == null) return false;
		return domain.checkAdd(configId, value);
	}
	
	@Override
	public boolean checkEnough(long playerId, Integer configId, Integer value) {
		ItemDomain domain = itemManager.getDomain(playerId);
		if (domain == null) return false;
		return domain.checkEnough(configId, value);
	}

	@Override
	public void reward(long playerId, Integer configId, Integer value, NatureEnum nEnum) {
		ItemDomain domain = itemManager.getDomain(playerId);
		if (domain == null)	return;
		//背包加入普通道具
		domain.addItem(configId, value, nEnum);
	}

	@Override
	public void cost(long playerId, Integer configId, Integer value, NatureEnum nEnum) {
		ItemDomain domain = itemManager.getDomain(playerId);
		if (domain == null)	return;
		domain.deductItemByConfigId(configId, value, nEnum);
	}
	
	@Override
	public void cost(long playerId, Long id, NatureEnum nEnum) {
		ItemDomain domain = itemManager.getDomain(playerId);
		if (domain == null) {
			return;
		}
		/*
		 * 道具默认有数量概念, 唯一的物品数量默认为1.
		 * 所以唯一道具,跟普通道具在扣除上面,逻辑相同,当数量为0则清除.
		 * 这里参数就默认扣除未1
		 */
		domain.deductItemById(id, 1, nEnum);
	}
}